﻿using System.Data;
using System.Data.SqlClient;

namespace ZKServer
{
    class DatabaseHelper
    {
        public void test(string ip,string username,string password)
        {
            using (SqlConnection connection = new SqlConnection(string.Format("Server={0};Uid={1};Pwd={2};Database=onecard", ip, username, password)))
            {
                connection.Open();
                connection.Close();
            }
        }

        public DataSet query(string ip,string username,string password,string sql)
        {
            using (SqlConnection connection = new SqlConnection(string.Format("Server={0};Uid={1};Pwd={2};Database=onecard", ip, username, password)))
            {
                DataSet ds = new DataSet();
                using (SqlDataAdapter sd = new SqlDataAdapter(sql, connection))
                {
                    // 这里数据将被批量读入
                    sd.Fill(ds);
                }
                return ds;
            }
        }

        public void execute(string ip, string username, string password, string sql,SqlParameter[] sqlParameterList)
        {
            using (SqlConnection connection = new SqlConnection(string.Format("Server={0};Uid={1};Pwd={2};Database=onecard", ip, username, password)))
            {
                connection.Open();
                SqlCommand com = new SqlCommand(sql, connection);
                com.CommandType = CommandType.Text;

                if(sqlParameterList != null && sqlParameterList.Length > 0)
                {
                    com.Parameters.AddRange(sqlParameterList);
                }

                com.ExecuteNonQuery();
                connection.Close();
            }
        }
    } 
}
